package cn.sirun.com.fca.map.presenter.impl;

import com.tencent.cloud.iov.kernel.model.BaseResponse;
import com.tencent.cloud.iov.util.rx.SimpleObserver;

import cn.sirun.com.fca.map.constant.MapActionConst;
import cn.sirun.com.fca.map.loader.MapLoader;
import cn.sirun.com.fca.map.model.repose.MapCarLocation;
import cn.sirun.com.fca.map.model.repose.MapCollectedResponse;
import cn.sirun.com.fca.map.model.repose.MapHomeCompanyResponse;
import cn.sirun.com.fca.map.model.request.MapAddCollectedRequest;
import cn.sirun.com.fca.map.model.request.MapCollectedRequest;
import cn.sirun.com.fca.map.model.request.MapDeleteCollected;
import cn.sirun.com.fca.map.model.request.MapSendCarRequest;
import cn.sirun.com.fca.map.presenter.IMapPresenter;
import cn.sirun.com.fca.map.presenter.MapFlowPresenter;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Descrip:
 * Author: Administrator
 * Time: 2018/12/2
 * Version:
 */

public class MapPresenter extends MapFlowPresenter<MapLoader> implements IMapPresenter {
    private BehaviorSubject<BaseResponse> mBehaviorSubject = BehaviorSubject.create();

    @Override
    public void getAddressName() {

    }

    @Override
    public void collectedAddress(String din,String startPage,String pageSize) {
        getLoader().mapcollected(din, startPage, pageSize)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<MapCollectedResponse>>() {
                    @Override
                    public void onNext(BaseResponse<MapCollectedResponse> collectedResponse) {
                        super.onNext(collectedResponse);
                      onAction(
                                MapActionConst.Normal.ACTION_MAP_COLLECTED_RESPONSE,
                              collectedResponse);
                    }
                });
    }

    @Override
    public void addCollectAddress(String din, MapAddCollectedRequest mapAddCollectedRequest) {
        getLoader().mapaddcollect(din, mapAddCollectedRequest)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_ADD_COLLECTED_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void deleteCollectAddress(String din, String favoriteAddressId) {
        getLoader().mapDeleteCollected(din, new MapDeleteCollected(favoriteAddressId))
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse>() {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_DELETE_COLLECTED_RESPONSE,
                                baseResponse);
                    }
                });
    }

    @Override
    public void getHomeAddress(String din) {
        getLoader().mapHomeCompany(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<MapHomeCompanyResponse>>() {
                    @Override
                    public void onNext(BaseResponse<MapHomeCompanyResponse> homeCompanyResponse) {
                        super.onNext(homeCompanyResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_HOME_COMPANY_RESPONSE,
                                homeCompanyResponse);
                    }
                });
    }

    @Override
    public void mapSendToCar(String din, MapSendCarRequest mapSendCarRequest) {
        getLoader().mapSendToCar(din,mapSendCarRequest)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<BaseResponse>>() {
                    @Override
                    public void onNext(BaseResponse<BaseResponse> sendCarResponse) {
                        super.onNext(sendCarResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_SEND_CAR_RESPONSE,
                                sendCarResponse);
                    }
                });
    }

    @Override
    public void getLastLocation(String din) {
        getLoader().getLastLocation(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<MapCarLocation>>() {
                    @Override
                    public void onNext(BaseResponse<MapCarLocation> mapCarLocationResponse) {
                        super.onNext(mapCarLocationResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_GET_CAR_RESPONSE,
                                mapCarLocationResponse);
                    }
                });
    }

    @Override
    public void updateLocation(String din) {
        getLoader().updateLocation(din)
                .takeUntil(mBehaviorSubject)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SimpleObserver<BaseResponse<MapCarLocation>>() {
                    @Override
                    public void onNext(BaseResponse<MapCarLocation> locationResponse) {
                        super.onNext(locationResponse);
                        onAction(
                                MapActionConst.Normal.ACTION_MAP_UPDATE_CAR_RESPONSE,
                                locationResponse);
                    }
                });
    }
    @Override
    protected MapLoader createLoader() {
        return new MapLoader();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mBehaviorSubject.onComplete();
    }
}
